package com.sim.stream.service.impl;


import com.sim.core.packets.Command;
import com.sim.core.packets.StreamMessageBody;
import com.sim.stream.service.StreamService;

import com.sim.stream.simclient.ImStreamClient;
import com.sim.stream.util.ShellUtil;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;


/**
 * 观众端全拉流方案
 * @author yzf
 */
@Service
public class StreamServiceImpl implements StreamService {

    private static final Logger LOG = LoggerFactory.getLogger(StreamServiceImpl.class.getName());
    @Resource
    private ShellUtil shellUtil;

    @Resource
    private ImStreamClient client;

    @Override
    public void publish(Map<String, String> map) {
        try {
            String name = map.get("name");
            Pair<String, String> groupUser = shellUtil.getGroupAndUser(name);
            StreamMessageBody message = new StreamMessageBody();
            message.setGroupId(groupUser.getLeft());
            message.setUserId(groupUser.getRight());
            message.setIsSystem(true);
            message.setStatus("start".equals(map.get("stage")));
            message.setCmd(Command.COMMAND_STREAM_REQ.getNumber());
            client.send(message);
        } catch (Exception e) {
            LOG.error("rtmp stream publish handle exception,reason:{}", e.toString());
        }
    }

    @Override
    public void publishDone(String requestStr) {
        try {
            //client.send(init(requestStr,false));
        } catch (Exception e) {
            LOG.error("rtmp stream publish handle exception,reason:{}", e.toString());
        }
    }


    @Override
    public void play(String requestStr) {

    }

    @Override
    public void playDone(String requestStr) {

    }
}
